package cn.wawi.controller.sys;

import java.util.List;
import javax.annotation.Resource;
import cn.wawi.common.R;
import cn.wawi.controller.BaseController;
import cn.wawi.dao.sys.ResourcesDao;
import cn.wawi.entity.sys.Resources;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * controller继承 BaseController 拥有
 * findAll       查询所有不分页
 * findOne       查询一条记录
 * findAllByPage 查询所有，分页
 * deleteBatch 批量删除
 * updateBatch 批量更新状态
 * deleteOne 根据主键删除
 * saveORupdate 主键id不为空时更新,否则保存
 */
@Controller
@RequestMapping("/sys_resources")
public class ResourcesController extends BaseController<Resources>{
	@Resource
	protected ResourcesDao<Resources> resourcesDao;
	
	@ResponseBody
	@RequestMapping(value="/getRoleResources",method={RequestMethod.GET,RequestMethod.POST})
	public R<Resources> getRoleResource(@RequestParam(value="roleId",required=true)int roleId){
		List<Resources> list=resourcesDao.getRoleResource(roleId);
		return success("请求成功!").put(list, list.size());
	}

	@ResponseBody
	@RequestMapping(value="/saveRoleResources",method={RequestMethod.POST})
	@Transactional(rollbackFor=Exception.class)
	public R<Resources> saveRoleResource(@RequestParam(value="roleId",required=true)Integer roleId,@RequestParam(value="resourcesIds[]")Integer[] resourceIds){
		resourcesDao.deleteRoleResources(roleId);
		for (int resourceId: resourceIds) {
			resourcesDao.saveRoleResources(roleId,resourceId);	
		}
		return success("保存角色资源成功!");
	}
	@ResponseBody
	@RequestMapping(value="/deleteRoleResources",method={RequestMethod.POST})
	public R<Resources> deleteRoleResources(@RequestParam(value="roleId",required=true)Integer roleId){
		resourcesDao.deleteRoleResources(roleId);
		return success("删除角色资源成功!");
	}
	
	@ResponseBody
	@RequestMapping(value="/saveBasePermission",method={RequestMethod.POST})
	@Transactional(rollbackFor=Exception.class)
	public R<Resources> saveBasePermission(@RequestParam(value="parentId",required=true)Integer parentId,Integer level){
		Resources save=new Resources("添加","create","icon-add",parentId,level,1);
		Resources update=new Resources("修改","update","icon-edit",parentId,level,2);
		Resources delete=new Resources("删除","delete","icon-remove",parentId,level,3);
		Resources list=new Resources("查询","list","icon-search",parentId,level,4);
		resourcesDao.saveOne(save);
		resourcesDao.saveOne(update);
		resourcesDao.saveOne(delete);
		resourcesDao.saveOne(list);
		return success("添加基本权限成功!");
	}
}
